<?php

namespace app\index\controller;
use think\Controller;
class Project extends Controller
{
    public function _initialize()
    {
        if (!cookie('uid')) {
            return $this->result('', 1, '您还未登陆');
        }
    }
    public function index()
    {

    }

    //添加打卡项目
    public function addProject()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $project = model('Project');
            $user = model('User');
            $uid = cookie('uid');
/*            if(!$user->hasmoney($data['price'])){
                return $this->result('', 1, '账户余额不足');
            }*/
            $project_count = $project->getProjectCount();
            if (!$project->projectIsExists()) {
                $datas = [
                    'uid' => cookie('uid'),
                    'money' => $data['price'],
                    'uniy_count' => $data['price'] / 10,
                    'join_count' => $project_count + 1,
                    'begin_time' => getSignTime(time()),
                    'end_time' => getEndDate(21),
                    'add_time' => time()
                ];

                $res = $project->insertGetId($datas);

                if ($res) {
                    $aa = $user->where(array('id' => $uid))->setInc('expend', $data['price']);
                    $cc = $user->where(array('id' => $uid))->setDec('balance', $data['price']);
                    $bb = $user->where(array('id' => $uid))->setInc('join_count', 1);
                    /*增加打卡子项*/
                    $project_item = model('Projectitem');
                    $create_time = time();
                    $sign_time = getSignTime($create_time);
                    $item_data = [
                        'pro_id' => $res,
                        'uid' => cookie('uid'),
                        'uniy_count' => $data['price'] / 10,
                        'join_money' => $data['price'],
                        'sign_time' => $sign_time,
                        'signbegin_time' => $create_time + $sign_time,
                        'signed_count' => 1,
                        'create_time' => $create_time
                    ];
                    $item_res = $project_item->insert($item_data);
                    return $this->result('', 0, '支付成功');
                } else {
                    return $this->result('', 1, '支付失败');
                }
            }else {
                return $this->result('', 1, '正在参与活动无法继续参与');
            }
        }
    }
    //追加项目资金
    public function add_to_money(){
        if (request()->isPost()) {
            $data = input('post.');
            $project = model('Project');
            $projectitem = model('Projectitem');
            $user = model('User');
            $uid = cookie('uid');
//            2018年7月14日16:56:56 修改
            /*if(!$user->hasmoney($data['price'])){
                return $this->result('', 1, '账户余额不足');
            }*/
            if (!$project->projectIsExists()){
                return $this->result('', 1, '暂无挑战项');
            } else {
                $proInfo = $project->getProject();
                $money=$proInfo['money']+$data['price'];

               // print_r($proInfo);exit;
                $where=[
                    'uid'=>cookie('uid'),
                    'status'=>0
                ];
                $userwhere = [
                    'id' => cookie('uid'),
                    'status'=>0
                ];
                $datas = [
                    'money' => $money,
                    'uniy_count' =>$money/ 10,
                ];
                $itemdata = [
                    'join_money' => $money,
                    'uniy_count' => $money/10
                ];
                $res = $project->where($where)->update($datas);
//                修改当天打卡的金额 获取当天的打卡的子项
               $currcard = $projectitem->getItem();
               if($currcard){
                   $projectitem->where('id',$currcard['id'])->update($itemdata);
               }
//                2018年7月14日15:48:16 修改 用户支出字段递增
                $user->where($userwhere)->setInc('expend',$data['price']);
                $user->where($userwhere)->setDec('balance',$data['price']);
                if ($res) {
                    return $this->result('', 0, '追加成功');
                } else {
                    return $this->result('', 1, '追加失败');
                }
            }
        }
    }
    //签到
    public function sign_in()
    {
        if (request()->isPost()) {
            $project = model('Project');
            $projectItem = model('Projectitem');
            //1.判断是否在指定的打卡时间
            /*echo $beginToday=mktime(06,30,0,date('m'),date('d'),date('Y'));
            echo $endToday=mktime(07,30,0,date('m'),date('d'),date('Y'));*/
            //2.判断是否存在项目
            if (!$project->projectIsExists()) {
                return $this->result('', 1, '您还未参与活动');
            }
            //4.结束今日签到项，累计签到天数
            $itemwhere = [
                'uid' => cookie('uid'),
                'status' => 0,
            ];
            $itemres = $projectItem->where($itemwhere)->update([
                'sign_date' => time(),
                'status' => 1,
            ]);
            //print_r($itemres);exit;
            if ($itemres) {
                //判断是否完成
                if ($project->isFull()) {
                    //1.退回当前项目奖金
                    $returnRes = $project->returnMoney();
                    //2.结束本次活动
                    $closeRes = $project->closeProject();
                    //3.返回提示
                    if ($returnRes && $closeRes) {
                        return $this->result('', 0, '恭喜你完成本次挑战！！');
                    }
                }
                $projectres = $project->where($itemwhere)->setInc('signed_count', 1);
                if ($projectres) {
                    //1.创建新的打卡項
                    $nowProject = $project->getProject();
                    if ($nowProject) {
                        //2.结束当前项目
                        $create_time = time();//创建时间
                        $sign_time = getSignTime($create_time);//打卡剩余秒数
                        $add_item_data = [
                            'pro_id' => $nowProject['id'],
                            'uid' => $nowProject['uid'],
                            'sign_time' => $sign_time,
                            'join_money' => $nowProject['money'],
                            'uniy_count'=>$nowProject['money']/10,
                            'signed_count' => $nowProject['signed_count'],
                            'signbegin_time' => $create_time + $sign_time,
                            'create_time' => $create_time,
                        ];
                        $additem_res = $projectItem->insertGetId($add_item_data);//新增明日签到项目
                        if ($additem_res) {

                            return $this->result('', 0, '签到成功');
                        } else {
                            return $this->result('', 1, '签到失败');
                        }
                    } else {
                        return $this->result('', 1, '挑战已经结束');
                    }


                } else {
                    return $this->result('', 1, '签到失败1');
                }
            } else {
                return $this->result('', 1, '签到失败2');
            }
        }
    }


}